package tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Properties;

public class GHTMLUtils {
	/**
	 * @author galip.erdem
	 * @param webSiteURL
	 *            : Ex.
	 *            "http://sample.gevezecafe.com/orderPrintOperation/printableOrders.php"
	 * @return String : Returns the website resource as String
	 */
	public static String getOrdersFromHTML(String webSiteURL) {
		WebFile file = null;
		try {
			file = new WebFile(webSiteURL);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String MIME = file.getMIMEType();
		Object content = file.getContent();
		String html = null;
		if (MIME.equals("text/html") && content instanceof String) {
			html = (String) content;
			// System.out.println(html);
		}
		return html;
	}

	public static String readURl(String webSiteURL) {
		URL url = null;
		try {
			url = new URL(webSiteURL);
		} catch (MalformedURLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		URLConnection yc = null;
		try {
			yc = url.openConnection();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		BufferedReader in = null;

		try {
			in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String inputLine = null;
		StringBuffer htmlBuffer = new StringBuffer("");
		try {
			while ((inputLine = in.readLine()) != null) {
				System.out.println(inputLine);
				htmlBuffer.append(inputLine);
			}

			// Charset.forName("UTF-8").encode(htmlBuffer.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte[] b = htmlBuffer.toString().getBytes(Charset.forName("UTF-8"));
		String v = new String(b, Charset.forName("UTF-8"));
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter("C:/Users/galip.erdem/Desktop/htmlText.txt"));
			writer.write(v);

		} catch (IOException e) {
		} finally {
			try {
				if (writer != null)
					writer.close();
			} catch (IOException e) {
			}
		}

		return v;
	}

	/**
	 * Parses plain String variable with specified <b> SEPERATOR </b>constant
	 * and returns String array
	 * 
	 * @param htmlResult
	 * @return String[] tokens
	 * @author galip.erdem
	 */
	public static String[] parseHTMLOrders(String htmlResult) {
		htmlResult = htmlResult.trim();
		String[] tokens = htmlResult.split(Config.Seperator.getKey());
		return tokens;
	}

	public static void loadConfigFile() {
		Properties prop = new Properties();
		InputStream input = null;

		try {

			input = new FileInputStream("config.properties");

			// load a properties file
			prop.load(input);

			// Writing into Config enum
			Config.Seperator.setKey(prop.getProperty("SEPERATOR"));
			Config.URL_HOME.setKey(prop.getProperty("URL_HOME"));
			Config.URL_TARGET.setKey(prop.getProperty("URL_TARGET"));
			Config.PDF_HOME_DIRECTORY.setKey(prop.getProperty("PDF_HOME_DIRECTORY"));
			Config.PDF_TARGET_DIRECTORY.setKey(prop.getProperty("PDF_TARGET_DIRECTORY"));
			Config.LOG_FILE_DIRECTORY.setKey(prop.getProperty("LOG_FILE_DIRECTORY"));

			/*
			 * // get the property value and print it out
			 * System.out.println(Config.Seperator.getKey());
			 * System.out.println(prop.getProperty("URL_TARGET"));
			 * System.out.println(prop.getProperty("URL_HOME"));
			 * System.out.println(prop.getProperty("PDF_TARGET_DIRECTORY"));
			 */
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	public static void createConfigFile() {
		Properties prop = new Properties();
		OutputStream output = null;

		try {

			output = new FileOutputStream("config.properties");

			// set the properties value
			prop.setProperty("SEPERATOR", "[|]");
			prop.setProperty("URL_TARGET", "http://sample.gevezecafe.com/orderPrintOperation/printOrder.php?orderId=");
			prop.setProperty("URL_HOME", "http://sample.gevezecafe.com/orderPrintOperation/printableOrders.php");
			prop.setProperty("PDF_HOME_DIRECTORY", "C:/Users/galip.erdem/Desktop/");
			prop.setProperty("PDF_TARGET_DIRECTORY", "C:/Users/galip.erdem/Desktop/");
			prop.setProperty("LOG_FILE_DIRECTORY", "C:/Users/galip.erdem/Desktop/");

			// save properties to project root folder
			prop.store(output, null);

		} catch (IOException io) {
			io.printStackTrace();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}

}
